package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.Client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-03-12
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    //统计一天的注册人数
    @Override
    public void registerCount(String day) {
        //添加记录之前先删除
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到某天的注册人数
        R result = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) result.getData().get("countRegister");

        //把获取到的数据添加到统计分析表里
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(10,20));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(10,20));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(10,20));

        baseMapper.insert(statisticsDaily);

    }

    //图表显示，返回两部分数据，日期json数组，数量json数据
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回的有两部分数据：日期 和 日期对应数量
        //前端要求的数组对应后端的list集合
        //创建两个集合，一个日期，一个数量
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();

        //遍历集合然后封装
        for (StatisticsDaily statisticsDaily : staList) {
            //封装日期集合
            dateList.add(statisticsDaily.getDateCalculated());
            //封装数量集合
            switch (type){
                case "login_num":
                    numList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("date_calculated",dateList);
        map.put("numDataList",numList);
        return map;
    }
}
